using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Events;

/// --------------------------
///
///
///
///  cangmingcc:
///  3325485079@qq.com
/// --------------------------
public class ResourcesManager : MonoSingleton<ResourcesManager>
{
    ///资源读取路径
    ///1.Resources路径读取
    ///2.AB路径读取
    public readonly string resourcesFolder = "Resources/";
    public readonly string abFolder =Application.streamingAssetsPath;

    bool isABassets = false; //是否使用AB加载
    
    
    //存储AB包加载的资源
    Dictionary<string, AssetBundle> assetBundles;
    //存储已经加载的资源
    Dictionary<string,Object> assetsPre;
    
    
    

    public override void Awake()
    {
        base.Awake();
        Init();
    }

    void Init()
    {
        //初始化数据字典
        assetBundles = new Dictionary<string, AssetBundle>();
        assetsPre = new Dictionary<string, Object>();
        //
#if UNITY_EDITOR
        isABassets = false;
#elif UNITY_ANDROID 
        isABassets = true;
#endif
    }

    /// <summary>
    /// 同步加载
    /// </summary>
    /// <param name="path"></param>
    /// <typeparam name="T"></typeparam>
    public void ResLoad<T>(string path,string resName=null) where T : Object
    {
        Object obj;
        if (isABassets)
        {
           obj = ABLoadMgr.Instance.LoadAB<T>(path,resName);
           Debug.Log("AB资源存在，加载成功");
           
        }
        else
        {
            obj=Resources.Load<T>(path);
            Debug.Log("资源存在，加载成功");
        }
        if(obj==null)
        {
            Debug.LogError("资源不存在，加载失败");
            return;
        }

        // path == 表示存储的资源路径（类型）  obj == 资源加载的数据
        assetsPre.Add(path, obj);
        

    }

   
   
    
    /// <summary>
    /// 异步加载
    /// 使用 UnityAction 的原因：因为异步加载的时候用到了协程，协程没有办法返回参数（我不会，如果你有办法能让协程返回参数请告诉我）
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="callback">返回</param>
    /// <param name="abname">AB名称（可选参，不是AB加载不需要传）</param>
    /// <param name="resName"></param>
    /// <typeparam name="T">Object类型</typeparam>
    public void ResAsycnLoad<T>(string path,UnityAction<T> callback,string abname=null,string resName=null) where T : Object
    {
        if (isABassets)
        {
            ABLoadMgr.Instance.ASyncLoadAB<T>(path,abname,resName,callback);
        }
        else
        {
            StartCoroutine(AsycnLoad(path,callback));
        }
    }

    IEnumerator AsycnLoad<T>(string path,UnityAction<T> callback) where T : Object
    {
        ResourceRequest res = Resources.LoadAsync<T>(path);
        
        //如果异步加载玩数据再 
        yield return res;
        
        callback(res.asset as T);
        //加载完之后 加入字典集合当中
        assetsPre.Add(path, res.asset as Object);
        
    }
    
    
    /// <summary>
    /// 获取已经加载的资源
    /// </summary>
    /// <param name="path">要获取资源的键值（路径）</param>
    public T GetRes<T>(string path,bool isUnload=false) where T : Object
    {
        if (assetsPre.ContainsKey(path))
        {
            return assetsPre[path] as T;
        }
        else
        {
            
            Debug.LogError("资源未加载，无法获取");
            return null;
        }

    }
    
    
    /// <summary>
    /// 卸载加载完成(不再复用的)的资源
    /// </summary>
    public void UnloadResources(string path)
    {
        Object obj=assetsPre[path];
        
        Resources.UnloadAsset(obj);
        
    }
    
    /// <summary>
    /// 卸载全部资源
    /// 大概是再关闭游戏的时候调用（我自己的判断）
    /// </summary>
    public void UnloadAllResources()
    {
        Resources.UnloadUnusedAssets();
    }
    
}
